package com.sgb.kjwl.viewmodel.home;


import android.text.TextUtils;

import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.RecommendEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.utils.LocationUtil;
import com.sgb.kjwl.view.ui.activity.home.SearchFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SearchFragmentModel extends BaseViewModel {
    private SearchFragment searchFragment;
    private HomeRepertory repertory;
    private int pageNum;
    private int pageSize = 10;
    private int total;

    public SearchFragmentModel(SearchFragment searchFragment, HomeRepertory repertory) {
        this.searchFragment = searchFragment;
        this.repertory = repertory;
    }

    public void searchCategories(String searchType, String keyWord, boolean isLoadMore) {
        if (TextUtils.isEmpty(searchType) || TextUtils.isEmpty(keyWord)) {
            LogUtil.d("SearchFragModel", "searchCatogories failed , searchType or keyWord is empty");
            return;
        }
        if (isLoadMore) {
            pageNum++;
        } else {
            pageNum = 1;
        }
        Map map = new HashMap();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("areaCode", repertory.getAreaCode());
        map.put("code", searchType);
        map.put("keyword", keyWord);
        map.put("lon",  LocationUtil.getCurrentLongitude());
        map.put("lat",  LocationUtil.getCurrentLatitude());
//        DialogHelper.showProgressDialog(searchFragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getBigHomeAPI().searchGoods(map).enqueue(new Callback<BaseEntity<ProListEntity<List<RecommendEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<RecommendEntity>>>> call, Response<BaseEntity<ProListEntity<List<RecommendEntity>>>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null || response.body().getData().getList() == null) {
                        MToast.showToast(searchFragment.getActivity(), "暂无数据");
                        return;
                    }
                    total = response.body().getData().getTotalRow();
                    if (total > 0) {
                        if (searchFragment != null) {
                            if (isLoadMore) {
                                searchFragment.loadMore(response.body().getData().getList(), null, false);
                            } else {
                                searchFragment.refresh(response.body().getData().getList(), null, false);
                            }
                        }
                    } else {
                        searchFragment.empty();
                    }

                } else {
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<RecommendEntity>>>> call, Throwable t) {
                t.printStackTrace();
//                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
